package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Role;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.mapper.UserRolemapper;
import com.zhitong.money.services.api.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class RoleContro {
    @Autowired
    private IRoleService roleService;



    @GetMapping("/role/{id}")
    public AjaxResponse getRoleById(@PathVariable String id) throws Exception{
        Role roleById = roleService.findRoleById(id);
        return AjaxResponse.success(roleById);
    }

    @GetMapping("/roles")
    public AjaxResponse getAllRole() throws Exception{
        List<Role> allRole = roleService.findAllRole();
        return AjaxResponse.success("查询成功!");
    }

    @GetMapping("/perChecked/{id}")
    public AjaxResponse getPidsById(@PathVariable String id) throws Exception{
        List<String> pidsById = roleService.findPidsById(id);
        return AjaxResponse.success(pidsById);
    }

    @PostMapping("/roleMap")
    public AjaxResponse getRoleByMap(@RequestBody Map<String,Object> map) throws Exception{
        List<Role> roleByMap = roleService.findRoleByMap(map);
        return AjaxResponse.success(roleByMap);
    }

    @PostMapping("/roles")
    public AjaxResponse getRoleByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo pageInfo = roleService.findRoleByPage(map);
        return AjaxResponse.success(pageInfo);
    }

    @RequiresPermissions({"addRole"})
    @PostMapping("/role")
    public AjaxResponse addRole(@RequestBody Map<String,Object> map) throws Exception{
            roleService.addRole(map);
            return AjaxResponse.success("添加角色成功!");
    }

    @RequiresPermissions({"updateRole"})
    @PutMapping("/role")
    public AjaxResponse updateRole(@RequestBody Map<String,Object> map) throws Exception{
            roleService.updateRole(map);
            return AjaxResponse.success("修改角色成功!");
    }

    @RequiresPermissions({"delRole"})
    @DeleteMapping("/role/{id}")
    public AjaxResponse delRole(@PathVariable String id) throws Exception{
        int i = roleService.delRole(id);
        if (i>0){
            return AjaxResponse.success("删除角色成功!");
        }else{
            return AjaxResponse.fail("删除角色失败!角色被关联");
        }

    }

    @RequiresPermissions({"delRole"})
    @PostMapping("/delRoles")
    public AjaxResponse delRolesByIds(@RequestBody List<String> ids) throws Exception{
        if (ids==null || ids.isEmpty()){
            return AjaxResponse.fail("请选择角色");
        }
        int i = roleService.delRolesByIds(ids);
        if (i>0){
            return AjaxResponse.success("删除角色成功!");
        }else{
            return AjaxResponse.fail("删除角色失败!角色被关联");
        }
    }

}
